/**
 * Copyright (c) 2010-2011 julewa.com.  
 * All rights reserved. 
 * 
 * @author Huang Benhua
 * @date 2011-2-20
 * 
**/
package com.julewa.loading;

import java.util.ResourceBundle;

import org.apache.struts2.convention.ConventionsServiceImpl;
import org.apache.struts2.convention.annotation.AnnotationTools;
import org.apache.struts2.convention.annotation.ResultPath;
import org.apache.struts2.convention.annotation.Standard;

import com.opensymphony.xwork2.config.ConfigurationException;

//@Inject("struts.convention.result.path") 
public class ConventionsSrvImpl extends ConventionsServiceImpl { 
	static String PATH = SystemUtils.getProperty("struts.convention.result.path");
	String PKG  = SystemUtils.getProperty("struts.convention.default.locators.basePackage") + ".";
	String ACTS[] = SystemUtils.getProperty("struts.convention.default.locators").split(".");
	//SEOActionNameBuilder namebuilder;
	public ConventionsSrvImpl() {
		super(PATH);
		for(int i = 0; i < ACTS.length; i ++){
			ACTS[i] = "." + ACTS[i] + ".";
		}
		//namebuilder = new SEOActionNameBuilder(
		//	SystemUtils.getProperty("struts.convention.action.name.lowercase"),
		//	SystemUtils.getProperty("struts.convention.action.name.separator")
		//);
		//namebuilder.setActionSuffix(SystemUtils.getProperty("struts.convention.action.suffix"));
	}

	public String determineResultPath(Class<?> actionClass) {
		ResultPath resultPathAnnotation = AnnotationTools.findAnnotation(
				actionClass, ResultPath.class);
		if (resultPathAnnotation == null){
			Standard std = actionClass.getAnnotation(Standard.class);
			if(std == null)return PATH;
			return getDefaultPath(actionClass);
		}
		String localResultPath = null;
		String property = resultPathAnnotation.property();
		if (property.equals("")) {
			localResultPath = resultPathAnnotation.value();
		} else {
			try {
				ResourceBundle strutsBundle = ResourceBundle
						.getBundle("struts");
				localResultPath = strutsBundle.getString(property);
			} catch (Exception e) {
				throw new ConfigurationException(
					"The action class ["
					+ actionClass
					+ "] defines"
					+ " a @ResultPath annotation and a property definition however the"
					+ " struts.properties could not be found in the classpath using ResourceBundle"
					+ " OR the bundle exists but the property ["
					+ property + "] is not defined.", e);
			}
		}
		//
		if(localResultPath == null)localResultPath = "";
		localResultPath = localResultPath.trim();
		if("".equals(localResultPath)){
			return getDefaultPath(actionClass);
		}
		if(localResultPath.startsWith("/"))return localResultPath;
		return  PATH + "/" + localResultPath;
	}
	
	private String getDefaultPath(Class<?> actionClass){
		StringBuffer buf = new StringBuffer(PATH).append("/");
		String pkg = actionClass.getPackage().getName();
		if(!pkg.startsWith(PKG))return buf.toString();
		pkg = pkg.substring(PKG.length() - 1);
		for(String a:ACTS){
			int i = pkg.indexOf(a);
			if(i < 1)continue;
			buf.append(pkg.substring(i + a.length()).replace('.', '/'));
			buf.append("/");
			break;
		}
		return buf.toString();
	}


}
